//
//  Bank.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/18.
//

#import "Bank.h"
#import "Text.h"
#import "Select.h"
#import "InfoModel.h"
#import "SelectPop.h"

@interface Bank ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong)UIButton *apply;
@property (nonatomic,strong)NSMutableArray *items;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation Bank

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self bankSore];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navtitleText = @"ldentity information";
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgIV.image = [UIImage imageNamed:@"bankbg1"];
    [self.view addSubview:bgIV];
    [self navUI];
    [self setupUI];
}
-(NSMutableArray *)items
{
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}

-(void)setupUI
{
    [self.view addSubview:self.apply];
    [_apply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-bottomSafeHeight-5);
        make.left.mas_offset(24);
        make.right.mas_offset(-24);
        make.height.mas_equalTo(52);
    }];
    UIView *view = [UIView new];
    view.backgroundColor = UIColor.whiteColor;
    view.layer.cornerRadius = 16;
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_apply.mas_top).offset(-20);
        make.left.mas_offset(13);
        make.right.mas_offset(-13);
        make.top.mas_offset(98+64+MarginTop+20);
    }];
    [view addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
    }];
    [_apply addTarget:self action:@selector(applyNext) forControlEvents:UIControlEventTouchUpInside];
}

-(void)applyNext
{
//    /cham/yankees
    NSDictionary *params = @{@"gossip":_pid};
    [DataFrameMove frameMoverForPost:@"/cham/yankees" MoverPost:YES frameParm:params finish:^(id  _Nonnull info) {
        
    }];
}

-(void)bankSore{
//    /cham/ofamber
    NSDictionary *params = @{@"gossip":_pid};
    [DataFrameMove frameMoverForPost:@"/cham/ofamber" MoverPost:YES frameParm:params finish:^(id  _Nonnull info) {
        NSArray *items = info[@"fashion"][@"kick"];
        [items enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            InfoModel *model = [InfoModel new];
            model.supposing = CPS(obj[@"supposing"]) ;
            model.soldierlylooking = CPS(obj[@"soldierlylooking"]);
            model.gardening = CPS(obj[@"gardening"]);
            model.youre = CPS(obj[@"youre"]);
            model.mosquito = CPS(obj[@"mosquito"]);
            model.delivered = obj[@"delivered"];
            model.inquired = CPS(obj[@"inquired"]);
            [self.items addObject:model];
        }];
        [self.tableView reloadData];
    }];

}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    __block InfoModel *model = self.items[textField.tag];
    if ([model.youre isEqualToString:@"Forbesa"]) {
        [[SelectPop getInstance] oneSelect:model key:1];
        [SelectPop getInstance].commitAction = ^(BRResultModel * _Nonnull model1) {
            model.herin = model1.key;
            model.inquired = model1.value;
            [self.items replaceObjectAtIndex:textField.tag withObject:model];
            [self->_tableView reloadData];
        };
        [self.view endEditing:YES];
        return NO;
    }else  if ([model.youre isEqualToString:@"Forbesb"]) {
        return YES;
    }else  if ([model.youre isEqualToString:@"Forbesc"]) {
        [self cityTest:textField.tag];
        [self.view endEditing:YES];
        return NO;
    }
    return YES;
}
-(void)cityTest:(NSInteger)index{
    [DataFrameMove frameMoverForPost:@"/cham/room" MoverPost:NO frameParm:@{} finish:^(id  _Nonnull info) {
        InfoModel *model = self.items[index];
        [[SelectPop getInstance] citySelect:info[@"fashion"] name:model.inquired];
        [SelectPop getInstance].addressCommitAction = ^(NSArray<BRResultModel *> * _Nonnull resultModelArr) {
            if (resultModelArr.count == 2) {
                model.inquired = [NSString stringWithFormat:@"%@-%@",resultModelArr[0].value,resultModelArr[1].value];
            }else{
                model.inquired = [NSString stringWithFormat:@"%@-%@-%@",resultModelArr[0].value,resultModelArr[1].value,resultModelArr[2].value];
            }
            [self.items replaceObjectAtIndex:index withObject:model];
            [self->_tableView reloadData];
            NSLog(@"%@",resultModelArr);
        };
    }];
}

-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    __block InfoModel *model = self.items[textField.tag];
    if ([model.youre isEqualToString:@"Forbesb"]) {
        model.inquired = textField.text;
        [_tableView reloadData];
        return YES;
    }
    return NO;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.items.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Select *cell =  [[Select alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([Select class])];
    cell.backgroundColor = UIColor.clearColor;
    InfoModel *model = self.items[indexPath.row];
    if (![model.inquired isEqualToString:@""]) {
        cell.name.text = model.inquired;
    }
    cell.titleLabel.text = model.supposing;
    cell.name.tag = indexPath.row;
    cell.name.delegate = self;
    cell.placeHolder = model.soldierlylooking;
    if ([model.youre isEqualToString:@"Forbesa"]) {
        cell.canEdit = NO;
        cell.detailImg.image = [UIImage imageNamed:@"down"];
    }else  if ([model.youre isEqualToString:@"Forbesb"]) {
        cell.canEdit = YES;
        cell.detailImg.image = [UIImage imageNamed:@"down"];
    }else  if ([model.youre isEqualToString:@"Forbesc"]) {
        cell.canEdit = NO;
        cell.detailImg.image = [UIImage imageNamed:@"phone"];
    }
    if (model.mosquito == 0) {
        cell.name.keyboardType = UIKeyboardTypeNumberPad;
    }else{
        cell.name.keyboardType = UIKeyboardTypeDefault;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(UIButton *)apply
{
    if (!_apply) {
        _apply = [UIButton buttonWithType:UIButtonTypeCustom];
        [_apply setImage:[UIImage imageNamed:@"applynow"] forState:UIControlStateNormal];
    }
    return _apply;
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.bounces = NO;
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
